﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Common;
using V5_Model;
using V5_BLL;

namespace V5_EShop.Admin.User
{
    public partial class User_Level : BasicPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Action = Input.SetString(Request["Action"]);
                switch (Action)
                {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        this.Bind_gvList();
                        break;
                }
            }
        }

        //绑定
        private void Bind_gvList()
        {
            this.MultiViewList.ActiveViewIndex = 0;
            int count = 0;
            this.gvList.DataSource = new BLLUserGroup().GetList(Pager.PageSize, Pager.CurrentPageIndex, "", ref count);
            Pager.RecordCount = count;
            this.gvList.DataBind();
        }

        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.Bind_gvList();
        }

        //编辑
        private void Bind_DataEdit(int id)
        {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelUserGroup model = new BLLUserGroup().GetModel(id);
            if (model != null)
            {
                this.txtID.Value = model.ID.ToString();
                this.txtGroupName.Text = model.GroupName;
                this.txtDiscount.Text = model.Discount;

                this.lblTitle.Text = "修改用户等级";
                this.btnSave.Text = "修改保存";
               
            }
        }
        //删除
        private void Bind_Delete(int id)
        {
            new BLLUserGroup().Delete(id);
            this.Bind_gvList();
        }
        //添加 修改
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int ID =Input.SetInt(this.txtID.Value);
            string GroupName = this.txtGroupName.Text;
            string Discount = this.txtDiscount.Text;
            BLLUserGroup bll = new BLLUserGroup();
            ModelUserGroup model = new ModelUserGroup();

            model.GroupName = GroupName;
            model.Discount = Discount;
            if (ID == 0)
            {
                model.ID = bll.GetMaxId();
                bll.Add(model);
            }
            else if (ID > 0)
            {
                model.ID = ID;
                bll.Update(model);
            }
            this.Bind_gvList();
        }
        //命令
        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string eName = Input.SetString(e.CommandName);
            int id = Input.SetInt(e.CommandArgument);
            switch (eName.Trim().ToLower())
            {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
                default:
                    break;
            }
        }
    }
}
